package com.homelink.android.explorerup.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.homelink.android.explorerup.R;
import com.homelink.android.explorerup.db.Bookmark;
import com.homelink.android.explorerup.view.NumOfImageView;

/**
 * 
 * @author hliang@126.com
 * 
 */
public class FavorListAdapter extends BaseAdapter {
	private ArrayList<Bookmark> mBookmarkList;
	private ArrayList<Bookmark> mHistoryList;
	private LayoutInflater mInflater;
	private Context mContext;

	public FavorListAdapter(Context ctx) {
		mContext = ctx;
		mInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public void setBookmarList(ArrayList<Bookmark> bookmark) {
		mBookmarkList = bookmark;
		if (mBookmarkList != null && mBookmarkList.size() > 0) {
			Bookmark bk = new Bookmark();
			bk.type = Bookmark.Types.BOOKMARK_TITLE;
			mBookmarkList.add(0, bk);
			bk = null;
		}
	}

	public void setHistory(ArrayList<Bookmark> history) {
		mHistoryList = history;
		if (mHistoryList != null && mHistoryList.size() > 0) {
			Bookmark bk = new Bookmark();
			bk.type = Bookmark.Types.MOST_TITLE;
			mHistoryList.add(0, bk);
			bk = null;
		}
	}

	public int getCount() {
		int len = 0;
		if (mBookmarkList != null) {
			len = mBookmarkList.size();
		}
		if (mHistoryList != null) {
			len += mHistoryList.size();
		}

		return len;
	}

	public Object getItem(int position) {
		int bookMarkSize = mBookmarkList == null ? 0 : mBookmarkList.size();
		if (bookMarkSize > 0 && position - bookMarkSize < 0) {
			return mBookmarkList.get(position);
		}

		int historySize = mHistoryList == null ? 0 : mHistoryList.size();

		if (historySize > 0) {

			return mHistoryList.get(position - bookMarkSize);
		}

		return null;

	}

	public long getItemId(int position) {
		Object obj = getItem(position);
		if (obj instanceof Bookmark) {
			return ((Bookmark) obj).id;
		} else {
			return 0;
		}
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		Object obj = getItem(position);
		View view = null;
		DataViewHolder holder = null;
		Bookmark bookmark = null;
		if (obj != null && obj instanceof Bookmark) {
			bookmark = (Bookmark) obj;

		} else {
			return null;
		}
		if (Bookmark.Types.BOOKMARK_TITLE == bookmark.type) {
			// FIXME: 还要优化
			TextView tv = new TextView(mContext);
			tv.setBackgroundColor(Color.GRAY);
			tv.setTextSize(25);
			tv.setText(R.string.msg_bookmark_title);
			return tv;
		}

		if (Bookmark.Types.MOST_TITLE == bookmark.type) {
			// FIXME: 还要优化
			TextView tv = new TextView(mContext);
			tv.setBackgroundColor(Color.GRAY);
			tv.setTextSize(25);
			tv.setText(R.string.msg_most_used_title);
			return tv;
		}

		if (convertView != null && convertView instanceof LinearLayout) {
			view = convertView;
			holder = (DataViewHolder) view.getTag();
		} else {
			view = mInflater.inflate(R.layout.fileitem, null);
			holder = new DataViewHolder();
			holder.imageView = (NumOfImageView) view
					.findViewById(R.id.fileIcon);
			holder.name = (TextView) view.findViewById(R.id.fileName);
			holder.path = (TextView) view.findViewById(R.id.filePath);
			view.setTag(holder);
		}

		holder.imageView.setNumber(0);
		holder.name.setText(bookmark.name);
		holder.path.setText(bookmark.path);

		return view;
	}

	class DataViewHolder {
		public NumOfImageView imageView;
		public TextView name;
		public TextView path;
	}

}
